//
//  FFlexibleDesignViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/9.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FFlexibleDesignViewController.h"
#import "SearchViewController.h"
#import "HMSegmentedControl.h"
#import "DesignAllCell.h"
#import "DesignSegment.h"
#import "UIView+Utils.h"
#import "Masonry.h"
#import "FFlexibleDesignView.h"


@interface FFlexibleDesignViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>


@property (nonatomic,assign)NSInteger   pageIndex;

///这是全部的tableView
@property (nonatomic,strong)  BaseTableView       * allTableView;
///这是设计师的tableView
@property (nonatomic,strong)  BaseTableView       * designerTableView;
///这是商品的tableView
@property (nonatomic,strong)  BaseTableView       * commodityTableView;
///这是创意空间的tableView
@property (nonatomic,strong)  BaseTableView       * createTableView;
@property (nonatomic,strong)  HMSegmentedControl  * segControl;
@property (nonatomic,strong)  UIScrollView        * centerScroll;
@property (nonatomic,strong)  DesignSegment       * bottomSegment;

@end

@implementation FFlexibleDesignViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavUI];
    [self setScrollViewUI];
    [self setSegmentViewUI];
    
}

#pragma mark - 设置选项卡的点击事件
-(void)segmentDidChangedValue
{
    self.bottomSegment.hidden = false;
    if (self.segControl.selectedSegmentIndex == 0) {
        self.bottomSegment.hidden = true;
        [self.centerScroll setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if(self.segControl.selectedSegmentIndex == 1){
        [self.centerScroll setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
        self.bottomSegment.titlesAry = @[@"综合",@"成交",@"好评",@"区域",@"筛选"];
        
    }else if(self.segControl.selectedSegmentIndex == 2){
        [self.centerScroll setContentOffset:CGPointMake(SCREEN_WIDTH * 2, 0) animated:YES];
        self.bottomSegment.titlesAry = @[@"综合",@"价格",@"区域",@"筛选"];
    }else{
        [self.centerScroll setContentOffset:CGPointMake(SCREEN_WIDTH * 3, 0) animated:YES];
         self.bottomSegment.titlesAry = @[@"综合",@"价格",@"区域",@"筛选"];
    }
}

#pragma mark - 实现scrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView) {
        NSInteger index = self.centerScroll.contentOffset.x/SCREEN_WIDTH + 0.5;
        self.segControl.selectedSegmentIndex = index;
    }
    else{
        CGPoint point = scrollView.contentOffset;
        if (point.x > 0) {
            scrollView.contentOffset = CGPointMake(0, point.y);//不要设置为CGPointMake(0, 0)，这样下面左右滑动的时候，就跳到起始位置，不科学
        }
    }
}

#pragma mark - 设置选项卡
-(void)setSegmentViewUI
{
    self.segControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"全部",@"设计师",@"商品",@"创意空间"]];
    self.segControl.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    self.segControl.backgroundColor = RGB_COLOR(237,34,59);
    self.segControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segControl.autoresizingMask = UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleRightMargin;
    self.segControl.titleTextAttributes = @{NSForegroundColorAttributeName:RGBACOLOR(255, 255, 255, 0.6),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:16]};
    //设置选项卡被选中的颜色
    self.segControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName:RGB_COLOR(255,255,255),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:16]};
    self.segControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    
    self.segControl.selectionIndicatorHeight = 2.5;
    self.segControl.selectionIndicatorColor = RGB_COLOR(255,255,255);
    //设置开始时默认选中的下标
    self.segControl.selectedSegmentIndex = 1;
    //设置选中的选项卡的点击事件
    [self.segControl addTarget:self action:@selector(segmentDidChangedValue) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segControl];
    
    self.bottomSegment = [DesignSegment segmentWithClick:^{
        [self segentChange];
    }];
    self.bottomSegment.frame = CGRectMake(0, self.segControl.bottom, SCREEN_WIDTH, 30);
    self.bottomSegment.titlesAry = @[@"综合",@"成交",@"好评",@"区域",@"筛选"];
    [self.view addSubview:self.bottomSegment];
    
    self.pageIndex = 1;
}


- (void)segentChange
{
    
}


- (void)setPageIndex:(NSInteger)pageIndex
{
    _pageIndex = pageIndex;
    
    if (pageIndex == 0) {
        self.centerScroll.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 40 - TAB_BAR_HEIGHT);
    }else{
        self.centerScroll.frame = CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70 - TAB_BAR_HEIGHT);
    }
}

-(void)setScrollViewUI
{
    self.centerScroll = [[UIScrollView alloc] init];
    self.centerScroll.frame = CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70 - TAB_BAR_HEIGHT);
    self.centerScroll.pagingEnabled = YES;
    self.centerScroll.showsVerticalScrollIndicator = NO;
    self.centerScroll.showsHorizontalScrollIndicator = NO;
    self.centerScroll.delegate = self;
    [self.centerScroll setContentSize:CGSizeMake(SCREEN_WIDTH * 4, 0)];
    [self.view addSubview:self.centerScroll];
    
    [self.centerScroll setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
    
    [self setTableViewUI];
    
}

-(void)setTableViewUI
{
    FFlexibleDesignView * allView = [[FFlexibleDesignView alloc] initWithType:1000];
    [self.centerScroll addSubview:allView];
    
    FFlexibleDesignView * designerView = [[FFlexibleDesignView alloc] initWithType:1001];
    [self.centerScroll addSubview:designerView];
    
    FFlexibleDesignView * commodityView = [[FFlexibleDesignView alloc] initWithType:1002];
    [self.centerScroll addSubview:commodityView];
    
    FFlexibleDesignView * createView = [[FFlexibleDesignView alloc] initWithType:1003];
    [self.centerScroll addSubview:createView];
    
    /*
    ///这是全部的tableView
    self.allTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.centerScroll.height)];
    self.allTableView.delegate = self;
    self.allTableView.dataSource = self;
    self.allTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.centerScroll addSubview:self.allTableView];
    
    ///这是设计师的tableView
    self.designerTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.centerScroll.height)];
    self.designerTableView.delegate = self;
    self.designerTableView.dataSource = self;
    self.designerTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.centerScroll addSubview:self.designerTableView];
    
    ///这是商品的tableView
    self.commodityTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, self.centerScroll.height)];
    self.commodityTableView.delegate = self;
    self.commodityTableView.dataSource = self;
    self.commodityTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.centerScroll addSubview:self.commodityTableView];
    
    ///这是创意空间的tableView
    self.createTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3, 0, SCREEN_WIDTH, self.centerScroll.height)];
    self.createTableView.delegate = self;
    self.createTableView.dataSource = self;
    self.createTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.centerScroll addSubview:self.createTableView];
    */
}

#pragma mark - 实现tableView的代理方法和数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 1001) {
        return 3;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1001) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return cell;
    }else if (tableView.tag == 1002){
        DesignAllCell *designAllcell = [tableView dequeueReusableCellWithIdentifier:@"DesignAllCell"];
        if (designAllcell == nil) {
            designAllcell = [DesignAllCell cellWithTableView:tableView andIndexPath:indexPath];
        }
        return designAllcell;
    }else if (tableView.tag == 1003){
        CommoditynCell *commoditynCell = [tableView dequeueReusableCellWithIdentifier:@"CommoditynCell"];
        if (commoditynCell == nil) {
            commoditynCell = [CommoditynCell cellWithTableView:tableView andIndexPath:indexPath];
        }
        return commoditynCell;
    }else{
        CreativeCell *creativeCell = [tableView dequeueReusableCellWithIdentifier:@"CreativeCell"];
        if (creativeCell == nil) {
            creativeCell = [CreativeCell cellWithTableView:tableView andIndexPath:indexPath];
        }
        return creativeCell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}

-(void)setNavUI
{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 52, 30)];
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 22, 22)];
    leftImg.image = [UIImage imageNamed:@"搜索"];
    [leftView addSubview:leftImg];
    
    UITextField *searchField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(322), 30)];
    searchField.placeholder = @"找专业的人才做专业的事情";
    searchField.layer.cornerRadius = ScreenX375(15);
    searchField.clipsToBounds = YES;
    searchField.backgroundColor = [UIColor whiteColor];
    searchField.leftViewMode = UITextFieldViewModeAlways;
    searchField.leftView = leftView;
    searchField.font = Kfont(13);
    self.navigationItem.titleView = searchField;
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(227), 30)];
    searchButton.backgroundColor = [UIColor clearColor];
    [searchButton addTarget:self action:@selector(turnToSearch) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem.titleView addSubview:searchButton];
}

- (void)turnToSearch {
    SearchViewController *vc = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}






@end
